package com.zzti.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.zzti.pojo.OrderEqu;
import com.zzti.pojo.SysResult;
import com.zzti.serviceImpl.EquipmentServiceImpl;
import com.zzti.serviceImpl.OrderEquServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Controller
public class OrderEquController {
    @Autowired
    private OrderEquServiceImpl orderEquService;

    @GetMapping("/ordEqu/selectByTime")
    @ResponseBody
    public String selectByTime() {
        int[] arr1 = new int[12];
        for (int i = 0; i < arr1.length; i++) {
            arr1[i] = i * 2;
        }
        int[] arr2 = orderEquService.selectByTime();
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(arr1);
        jsonArray.add(arr2);
        System.out.println(jsonArray);
        return jsonArray.toJSONString();
    }


    @Autowired
    private EquipmentServiceImpl equipmentService;

    @RequestMapping("/test1")
    @ResponseBody
    public String test1() {
        orderEquService.selectByTime();
        return null;
    }

    @GetMapping("/ordEqu/contOneMonthEqu")
    @ResponseBody
    public String contOneMonthEqu() {

        List<Map<String, Integer>> oemap = orderEquService.contOneMonthEqu();
        System.out.println(oemap);
        String[] s1 = new String[5];
        Integer[] s2 = new Integer[5];
        Map<String, Integer> genderCount = new HashMap<>();
        for (Map<String, Integer> kv : oemap) {
            String key = null;
            Integer value = null;

            for (Map.Entry<String, Integer> entry : kv.entrySet()) {
                if (entry.getKey().equals("key")) {
                    key = String.valueOf(entry.getValue());
                } else {
                    value = entry.getValue();
                }
            }
            genderCount.put(key, value);
        }
        System.out.println(genderCount);
        int con = 0;
        Set<String> strings = genderCount.keySet();
        System.out.println(strings);
        for (String sa : strings) {
            s1[con] = sa;
            s2[con] = genderCount.get(sa);
            con++;
        }
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(s1);
        jsonArray.add(s2);
        System.out.println(jsonArray);
        String result2 = jsonArray.toJSONString();
        return result2;
    }

    @ResponseBody
    @RequestMapping("/selectAll")
    public String selectAll() {

        List<OrderEqu> all = orderEquService.slectAll();

        String s = JSON.toJSONString(all);

        return "{\"count\":" + all.size() + ", \"code\":0 , \"data\":" + s + "}";

    }

    @ResponseBody
    @RequestMapping("/selectList")
    public String selectList(Integer page, int limit, HttpServletRequest request,
                             @RequestParam(name = "visiterName", required = false, defaultValue = "") String visiterName,
                             @RequestParam(name = "equName", required = false, defaultValue = "") String equName,
                             @RequestParam(name = "state", required = false, defaultValue = "") String state
    ) {

        Map<String, Object> ret = new HashMap<>();
        Map<String, Object> queryMap = new HashMap<>();

        queryMap.put("visiterName", visiterName);
        queryMap.put("equName", equName);
        if (state.equals("")) {
            state = null;
        }
        queryMap.put("state", state);
//        System.out.println(queryMap.get("damage").toString());
        List<OrderEqu> aaa = orderEquService.slectList(queryMap);

        queryMap.put("page", (page - 1) * limit);
        queryMap.put("limit", limit);
        System.out.println(visiterName);
        List<OrderEqu> all = orderEquService.slectList(queryMap);

        System.out.println(all);
        String s = JSON.toJSONString(all);
        System.out.println(s);
        return "{\"count\":" + aaa.size() + ", \"code\":0 , \"data\":" + s + "}";

    }

    @RequestMapping("/upDamage/{id},{state},{equId}")
    @ResponseBody
    public SysResult upDamage(@PathVariable("id") String id, @PathVariable("state") String state, @PathVariable("equId") String equId) {

        Map<String, Object> res = new HashMap();
        if (state.equals("1")) {
            Integer orde = orderEquService.updateState(id);
            System.out.println(orde);
            if (orde == 1)
                return SysResult.success();
            return SysResult.fail();
        } else if (state.equals("-1")) {
            Integer orde = orderEquService.updateDamage(id);
            Integer integer = equipmentService.changeEquDamage(equId);
            if (orde == 1 && integer == 1)
                return SysResult.success();
            return SysResult.fail();
        } else if (state.equals("2")) {
            Integer orde = orderEquService.updateDamage(id);
            Integer integer = equipmentService.changeEquDamage(equId);
            if (orde == 1 && integer == 1)
                return SysResult.success();
            return SysResult.fail();
        } else {
            return SysResult.fail();
        }
    }

    @RequestMapping("/delete/{id}")
    @ResponseBody
    public SysResult delete(@PathVariable("id") Integer[] id) {

        Integer orde = orderEquService.deleteByIds(id);
        System.out.println(orde);
        if (orde == 1)
            return SysResult.success();
        return SysResult.fail();
    }


}
